﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using Unscrambler.Controls;
using System.Windows.Media;

namespace Unscrambler.Comparers
{
    public class LetterTileComparer : IComparer<LetterTile>
    {
        private Rect _containerSize = Rect.Empty;
        
        // Summary:
        //     Compares two objects and returns a value indicating whether one is less than,
        //     equal to, or greater than the other.
        //
        // Parameters:
        //   x:
        //     The first object to compare.
        //
        //   y:
        //     The second object to compare.
        //
        // Returns:
        //     A signed integer that indicates the relative values of x and y, as shown
        //     in the following table.Value Meaning Less than zero x is less than y.Zero x
        //     equals y.Greater than zero x is greater than y.
        public int Compare(LetterTile x, LetterTile y)
        {

            //need to find the TranslateTransforms specifically, if you look only at the TransformGroup it takes into account
            //the rotatetransforms, and sometimes the Left value of the Rect will be messed up resulting in a bogus ordering.
            TransformGroup firstGroup = x.RenderTransform as TransformGroup;
            TransformGroup secondGroup = y.RenderTransform as TransformGroup;
            if (firstGroup != null && secondGroup != null)
            {
                return Convert.ToInt32(firstGroup.Children.OfType<TranslateTransform>().First<TranslateTransform>().TransformBounds(_containerSize).Left - 
                    secondGroup.Children.OfType<TranslateTransform>().First<TranslateTransform>().TransformBounds(_containerSize).Left);
            }

            throw new InvalidOperationException("All letter tiles must have transform groups defined");
            
        }

        public LetterTileComparer(Rect containingPanelSize)
        {
            _containerSize = containingPanelSize;
        }
    }
}
